<template>
  <div class="dashboard">
    <TopNavbar />
    <Sidebar />
    <div class="club-audit-content">
      <el-card class="box-card">
        <template #header>
          <div class="card-header">
            <span>社团审核</span>
          </div>
        </template>
        <div class="search-header">
          <el-form :inline="true" class="demo-form-inline">
            <el-form-item>
              <el-input
                v-model="searchQuery"
                placeholder="搜索社团名称"
                prefix-icon="el-icon-search"
                @change="handleSearch"
              ></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="resetSearch">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
        <div class="audit-actions">
          <el-tabs ><h4>待审核列表:</h4></el-tabs>
        </div>
        <el-table :data="clubs" style="width: 100%">
          <el-table-column prop="clubName" label="社团名称" width="180" header-align="center" align="center"></el-table-column>
          <el-table-column prop="applyTime" label="发布时间" width="180" header-align="center" align="center"></el-table-column>
          <el-table-column label="操作" header-align="center" align="center">
            <template #default="{ row }">
              <el-dropdown>
                <el-button type="text">
                  操作
                  <i class="el-icon-arrow-down el-icon--right"></i>
                </el-button>
                <template #dropdown>
                  <el-dropdown-menu>
                    <el-dropdown-item v-if="row.status === '待审核'" @click="auditClub(row)">审核</el-dropdown-item>
                    <el-dropdown-item @click="viewClub(row)">查看</el-dropdown-item>
                    <el-dropdown-item @click="deleteClub(row)">删除</el-dropdown-item>
                  </el-dropdown-menu>
                </template>
              </el-dropdown>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>

    <!-- 审核对话框 -->
    <el-dialog v-model="dialogVisible" title="审核申请" width="50%">
      <el-form :model="currentMessage" label-width="120px">
        <el-form-item label="社团ID">
          <span>{{ currentMessage.clubId }}</span>
        </el-form-item>
        <el-form-item label="申请内容">
          <el-input v-model="currentMessage.messageContent" type="textarea" :rows="4" readonly></el-input>
        </el-form-item>
        <el-form-item label="发送时间">
          <span>{{ formatDate(currentMessage.sentTime) }}</span>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="approveClub">通过</el-button>
          <el-button type="danger" @click="rejectClub">拒绝</el-button>
        </span>
      </template>
    </el-dialog>

    <el-dialog v-model="clubDetailDialogVisible" title="社团详情" width="50%">
      <el-descriptions v-if="clubDetail" :column="1" border>
        <el-descriptions-item label="Logo">
          <el-image
            style="width: 100px; height: 100px"
            :src="clubDetail.logoUrl"
            fit="cover"
          ></el-image>
        </el-descriptions-item>
        <el-descriptions-item label="社团ID">{{ clubDetail.clubId }}</el-descriptions-item>
        <el-descriptions-item label="社团名称">{{ clubDetail.clubName }}</el-descriptions-item>
        <el-descriptions-item label="社长ID">{{ clubDetail.leaderId }}</el-descriptions-item>
        <el-descriptions-item label="社长姓名">{{ clubDetail.leaderName }}</el-descriptions-item>
        <el-descriptions-item label="社团描述">{{ clubDetail.clubDescription }}</el-descriptions-item>
        <el-descriptions-item label="类别">{{ clubDetail.category }}</el-descriptions-item>
        <el-descriptions-item label="要求">{{ clubDetail.requirements }}</el-descriptions-item>
        <el-descriptions-item label="申请时间">{{ formatDate(clubDetail.applyTime) }}</el-descriptions-item>
        <el-descriptions-item label="现有人数">{{ clubDetail.currentMembers }}</el-descriptions-item>
        <el-descriptions-item label="发布时间">{{ formatDate(clubDetail.publishTime) }}</el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>


<script>
import Sidebar from '@/components/Sidebar.vue';
import TopNavbar from '@/components/TopNavbar.vue';
import axios from "axios";
// import { format } from 'date-fns'
import { ElDialog, ElDescriptions, ElDescriptionsItem, ElImage } from 'element-plus';

export default {
  name: 'ClubAuditPage',
  components: {
    Sidebar,
    TopNavbar,
    ElDialog,
    ElDescriptions,
    ElDescriptionsItem,
    ElImage,
  },
  data() {
    return {
      clubs: [],
      dialogVisible: false,
      currentMessage: {},
      currentPage: 1,
      pageSize: 10,
      clubDetailDialogVisible: false,
      clubDetail: null,
    };
  },
  methods: {
    logout() {
      console.log('用户退出登录');
    },
    async auditClub(club) {
      try {
        const response = await axios.get('/adminMessage/getMessage', {
          params: {
            clubId: club.clubId
          }
        });
        if (response.data.code === 0) {
          this.currentMessage = response.data.data;
          this.dialogVisible = true;
        } else {
          console.error('获取申请信息失败:', response.data.message);
        }
      } catch (error) {
        console.error('获取申请信息时发生错误:', error);
      }
    },
    async viewClub(club) {
      try {
        const response = await axios.get(`/club/approvalDetails/${club.clubId}`);
        if (response.data.code === 0) {
          this.clubDetail = response.data.data;
          this.clubDetailDialogVisible = true;
        } else {
          this.$message.error('获取社团详情失败');
        }
      } catch (error) {
        console.error('获取社团详情时发生错误:', error);
        this.$message.error('获取社团详情时发生错误');
      }
    },
    async deleteClub(club) {
      try {
        // 显示确认对话框
        await this.$confirm('确定要删除这个社团吗？', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        });
        const response = await axios.delete('/club/delete', {
          data: [club.clubId]
        });

        if (response.data.code === 0) {
          this.$message.success('社团删除成功');
          // 从列表中移除被删除的社团
          this.clubs = this.clubs.filter(c => c.clubId !== club.clubId);
        } else {
          this.$message.error(response.data.message || '社团删除失败');
        }
      } catch (error) {
        if (error !== 'cancel') { // 用户取消操作不显示错误
          console.error('删除社团时发生错误:', error);
          this.$message.error('删除社团时发生错误');
        }
      }
    },
    async fetchClubs() {
      try {
        const response = await axios.get('/club/inReview');
        if (response.data.code === 0) {
          this.clubs = response.data.data.map(club => ({
            clubId: club.clubId,
            clubName: club.clubName,
            leader: club.leaderName,
            category: club.category,
            applyTime: club.applyTime,
            status: '待审核'
          }));
        } else {
          console.error('获取社团列表失败:', response.data.message);
        }
      } catch (error) {
        console.error('获取社团列表时发生错误:', error);
      }
    },
    formatDate(dateString) {
      if (!dateString) return '未设置';
      return dateString;
      // return format(new Date(dateString), 'yyyy-MM-dd HH:mm:ss');
    },
    approveClub() {
      this.auditApplication(this.currentMessage.clubId,true)
      this.dialogVisible = false;
    },
    rejectClub() {
      this.auditApplication(this.currentMessage.clubId,false)
      this.dialogVisible = false;
    },
    async auditApplication(clubId,approve) {
      try {
        const response = await axios.post('/club/updateStatus',null,{params:{
          clubId:clubId,approve:approve
          }});
        if (response.data.code === 0) {
          if(approve===true){
            this.$message.success('已通过请求');
          }
          if(approve===false){
            this.$message.success('已拒绝请求');
          }
        } else {
          this.$message.error('处理失败');
        }
      } catch (error) {
        console.log(error)
      }
    },
  },
  created() {
    this.fetchClubs();
  }
};
</script>



<style scoped>
.dashboard {
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
  position: relative;
  background: #ffffff;
}

.top-nav {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 10;
}

.sidebar {
  width: 250px;
  position: fixed;
  left: 0;
  top: 60px;
  bottom: 0;
  overflow-y: auto;
  box-shadow: 2px 0 5px rgba(0, 0, 0, 0.1);
  background: #fff;
}

.club-audit-content {
  flex-grow: 1;
  margin-left: 250px;
  padding: 20px;
  padding-top: 80px; /* Adjusted to match the TopNavbar height */
  overflow-y: auto;
  overflow-x: hidden;
}

.box-card {
  margin-top: 20px;
  padding: 20px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}


.search-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.audit-actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.audit-actions .el-button {
  margin-right: 10px;
}

.audit-actions .el-button:last-child {
  margin-right: 0;
}

.el-table {
  margin-top: 10px;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
}

.dialog-footer .el-button {
  margin-left: 10px;
}

@media (max-width: 768px) {
  .top-nav {
    left: 0;
  }

  .sidebar {
    width: 100%;
    position: relative;
  }

  .club-audit-content {
    margin-left: 0;
    padding: 10px;
  }

  .el-row {
    margin-left: 0 !important;
    margin-right: 0 !important;
  }

  .el-col {
    padding-left: 0 !important;
    padding-right: 0 !important;
  }

  .el-button--primary {
    margin-top: 10px;
    width: 100%;
  }
}
</style>
